import dayjs from 'dayjs'
import Schema from 'validate'

const formInfo = new Schema({
  houseId: {
    type: String,
    required: true,
    message: '请选择需要维修的房屋'
  },
  repairItemId: { type: String, required: true, message: '请选择需要维修的项目' },
  mobile: { type: String, required: true, match: /^1[3-9]\d{9}$/, message: '请填写手机号' },
  appointment: { type: String, required: true, message: '请选择维修日期~' },
  description: { type: String, required: true, length: { min: 1, max: 200 }, message: '请选填写问题描述,字数在0~200' },
  attachment: {
    type: Array,
    length: { min: 0, max: 9 },
    each: {
      id: { type: String, required: true },
      url: { type: String, required: true }
    },
    message: '请选择图片'
  }
})
Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    // 可选房屋名称
    houseInfo: '',
    // 可选房屋id
    houseId: '',
    // 维修项目id
    repairItemId: '',
    // 维修项目名字
    repairItemName: '',
    // 手机号
    mobile: '',
    // 问题描述
    description: '',
    // 上门维修时间
    appointment: '',
    // 最小时间
    minDate: new Date().getTime(),
    // 最大时间
    // maxDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 3
    maxDate: +dayjs().add(3, 'day')
  },
  onLoad ({ id }) {
    if (id) {
      wx.setNavigationBarTitle({ title: '修改报修信息' })
      this.getRepairDetail(id)
    } else {
      this.getHouseLinst()
    }
    this.getRepairItem()
  },
  // 获取详情
  async getRepairDetail (id) {
    const res = await wx.http({ url: `/repair/${id}` })
    this.setData({ ...res.data })
  },
  // 获取可选房屋
  async getHouseLinst () {
    const res = await wx.http({ url: '/house' })
    console.log(res)
    this.setData({ houseList: res.data })
  },
  // 查询维修项目-选项
  async getRepairItem () {
    const res = await wx.http({ url: '/repairItem' })
    // console.log(res)
    this.setData({ repairItem: res.data })
  },
  // 获取选择的房屋名称和id
  onHouseSelect (e) {
    // console.log(e)
    const { name, id } = e.detail
    this.setData({
      houseInfo: name,
      houseId: id
    })
  },
  // 获取选择的维修项目
  onRepairItemSelect (e) {
    const { name, id } = e.detail
    this.setData({
      repairItemId: id,
      repairItemName: name
    })
  },
  // 获取选择的时间
  onSelectData (e) {
    // console.log(e.detail)
    this.setData({
      currentDate: e.detail,
      appointment: dayjs(e.detail).format('YYYY-MM-DD'),
      dateLayerVisible: false
    })
  },
  // 获取图片
  afterRead (e) {
    // console.log(e.detail.file.url)
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: e.detail.file.url,
      name: 'file',
      header: { Authorization: `Bearer ${getApp().token}` },
      success: res => {
        // console.log(res)
        const { data } = JSON.parse(res.data)
        console.log(data)
        this.setData({ attachment: [...this.data.attachment, data] })
      }
    })
  },
  // 删除图片
  onDelete (e) {
    // console.log(e.detail.index)
    wx.showModal({
      title: '删除',
      content: `确认删除第${e.detail.index + 1}张图片`,
      success: ({ confirm }) => {
        if (confirm) {
          // 获取图片列表
          const { attachment } = this.data
          // 点击的下标删除对应的图片
          attachment.splice(e.detail.index)
          // 将删除后的列表返回
          this.setData({ attachment })
        }
      }
    })
  },
  // 提交
  async goList () {
    const { attachment, houseId, repairItemId, mobile, description, appointment } = this.data
    const data = { attachment, houseId, repairItemId, mobile, description, appointment }

    const errors = formInfo.validate(data)
    if (errors.length) {
      return wx.utils.toast(errors[0].message)
    }
    if (this.data.id) {
      data.id = this.data.id
    }
    await wx.http({
      url: '/repair',
      method: 'POST',
      data
    })
    await wx.showToast({ title: '成功', icon: 'success' })
    setTimeout(() => {
      wx.navigateTo({ url: '/repair_pkg/pages/list/index' })
    }, 2000)
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },
  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  }
  // goList () {
  //
  // }
})
